package com.myhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myhome.entity.Menu;
import com.myhome.entity.MenuRole;
import com.myhome.entity.RespBean;
import com.myhome.service.IMenuRoleService;
import com.myhome.service.IMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-09-04
 */
@RestController
@RequestMapping("/system/cfg")
public class MenuController {
    @Autowired
    IMenuService menuService;
    @Autowired
    IMenuRoleService menuRoleService;

    @ApiOperation(value = "获取用户菜单")
    @GetMapping("/menu")
    public List<Menu> getMenuByUserId() {
        return menuService.getMenuByUserId();
    }

    @ApiOperation(value = "根据角色id查询菜单id")
    @GetMapping("/getmenuid/{id}")
    public List<Integer> getMenusIdByRoleId(@PathVariable("id") Integer rid) {
        return menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid", rid)).stream().map(MenuRole::getMid).collect(Collectors.toList());
    }

    @ApiOperation(value = "获取所有菜单")
    @GetMapping("/getallMenus")
    public List<Menu> getAllMenus() {
        return menuService.getAllmenus();
    }

    @ApiOperation(value = "修改权限菜单")
    @PostMapping("/updatemenus")
    public RespBean updateMenus(Integer rid,Integer[] mids){
        return menuRoleService.updateMenus(rid,mids);
    }
}
